/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package facade;


import entities.RequestXproduct;
import java.util.ArrayList;
import javax.ejb.EJB;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Andrés Felipe
 */
@Stateless
@Remote(RequestXproductFacadeRemote.class)
public class RequestXproductFacade extends AbstractFacade<RequestXproduct> implements RequestXproductFacadeRemote {
    @PersistenceContext(unitName = "Javeriana.EJB.EstimatorPU")
    private EntityManager em;

    @EJB
    private ProductFacadeRemote product;
    
    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public RequestXproductFacade() {
        super(RequestXproduct.class);
    }
    
    @Override
     public double getQuotePrice(int idRequest,ArrayList<ArrayList<Object>> customerRequest) {
          
        double quotePrice = 0;
        if (customerRequest.size() > 1) {
            for (ArrayList<Object> o : customerRequest) {
                if (customerRequest.indexOf(o) != 0) {
                    ArrayList<Object> content = (ArrayList<Object>) o;
                    RequestXproduct requestXproduct = new RequestXproduct(idRequest, Integer.parseInt(String.valueOf(content.get(0))));
                    requestXproduct.setQuantity(Double.valueOf(String.valueOf(content.get(2))).intValue());
                    this.create( requestXproduct); 
                    double price = product.find(Integer.parseInt(String.valueOf(content.get(0)))).getPrice();
                    //System.out.println("holaaaa" + price);
                    quotePrice += (Double.valueOf(String.valueOf(content.get(2))) * price);
                }
            }
        }
        return quotePrice;
    }
}
